package com.yizooo.tallybook.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.yizooo.tallybook.R;

public class BudgetDialog extends Dialog implements View.OnClickListener {
    ImageView cancelIv;
    Button ensureBtn;
    EditText moneyEt;
public interface OnEnsureListener {
    public void onEnsure(float money);
}
OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public BudgetDialog(@NonNull Context context) {
        super(context);
    }
@Override
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_budget);
       cancelIv=findViewById(R.id.dialog_budget_tv_error);
       ensureBtn=findViewById(R.id.dialog_budget_btn_ensure);
       moneyEt=findViewById(R.id.dialog_budget_et);
       cancelIv.setOnClickListener(this);
       ensureBtn.setOnClickListener(this);
       ensureBtn.setOnClickListener(this);

}
    @Override
    public void onClick(View view) {
        int viewId=view.getId();
        if (viewId==R.id.dialog_budget_tv_error){
            cancel();
        }else  if (viewId==R.id.dialog_budget_btn_ensure){
            String data=moneyEt.getText().toString();
            if(TextUtils.isEmpty(data)){
                Toast.makeText(getContext(),"输入数据不能为空!",Toast.LENGTH_SHORT).show();
                return ;
            }
            float money=Float.parseFloat(data);
            if(money<=0){
                Toast.makeText(getContext(),"预算金额必须大于0",Toast.LENGTH_SHORT).show();
                return ;
            }
            if(onEnsureListener!=null){
                onEnsureListener.onEnsure(money);
            }
            cancel();
        }
    }
}
